package chapter17.HomeWork;

/**
 * @Auther: Tx
 * @Date: 2022/7/13 16:38
 */

/**
 * 1.有两个用户分别从同一个卡上取钱（总额：10000）
 * 2.每次都取出1000，当余额不足时，就不能取款了
 * 3.不能出现超取问题
 */

//问题：继承自Thread和实现Runnable接口的synchronized的区别
public class HomeWork02 {
    public static void main(String[] args) {
        TakeMoney takeMoney0 = new TakeMoney();
        takeMoney0.start();


    }
}

class TakeMoney extends Thread {
    private static int money = 100000;

    public synchronized void take() {
        while (true) {
            if (money < 1000) {
                System.out.println("余额不足");
                return;
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            money -= 1000;
            System.out.println("线程" + Thread.currentThread().getName() + "取出了1000块" +
                    "现在余额为" + money);
        }
    }

    @Override
    public void run() {
        take();
    }
}
